/*
	jQuery Plugin spy (leftlogic.com/info/articles/jquery_spy)
	(c) 2006 Remy Sharp (leftlogic.com) jquery[at]leftlogic[dot]com
	$Id: spy.js,v 1.2 2006/08/14 10:07:18 remy Exp $
*/
var spyRunning = 1;

$.fn.spy = function(settings) {
	if (!settings['ajax'])
	{
		alert("An AJAX/AJAH URL must be set for the spy to work.");
		return;
	}
	
	var o = {
		limit: (settings['limit'] || 10),
		fadeLast: (settings['fadeLast'] || 5),
		ajax: settings['ajax'],
		timeout: (settings['timeout'] || 3000),
		push: (settings['push'] || -1),
		fadeInSpeed: (settings['fadeInSpeed'] || 'slow')
	};
	
	return this.each(function() {
		var e = this;
		var lr = '';
		var epoch = new Date(1970, 1, 1);
	    var now = new Date();
	    var timestamp = Math.floor((now - epoch) / 1000);
		e.timer = window.setInterval(function() {
			if (spyRunning)
			{
				$.post(o['ajax'], { 'timestamp': timestamp }, function(r) {
					if (r != lr) // don't show dupes
					{
						$('#' + e.id + ' > div:gt(' + (o['limit'] - 2) + ')').remove();
						$('#' + e.id + ' > div:gt(' + (o['limit'] - o['fadeLast'] - 2) + ')').fadeEachDown();
						if (o['push'] == -1){
							$('#' + e.id).prepend(r);
						}else
							o['push'].call(e, r);
						$('#' + e.id + ' > div:first').fadeIn(o['fadeInSpeed']);
						lr = r;					
					}
				});
				now = new Date();
			    timestamp = Math.floor((now - epoch) / 1000);
			}
		}, o['timeout']);
	});
};

$.fn.fadeEachDown = function() {
	var s = this.size();
	return this.each(function(i) {
		var o = 1 - (s == 1 ? 0.5 : 0.85/s*(i+1));
		var e = this.style;
		if (window.ActiveXObject)
			e.filter = "alpha(opacity=" + o*100 + ")";
	});
};

function pauseSpy() {
	spyRunning = 0; return false;
}

function playSpy() {
	spyRunning = 1; return false;
}
